package sharemem

import (
	"log"
//	"server_cluster/common/logger"
)
func (u *ShmUnit) BuildCfgSkillEffect(id int32) *Cfg_skill_effect {
	cfg_skill_effect := Get_config_skill_effect(id)
	if cfg_skill_effect == nil {
        log.Printf("config skill effect %d is not found.",id)
        return nil
	}

	tmp := &Cfg_skill_effect {
		target:cfg_skill_effect.Target,
		ctype:cfg_skill_effect.Ctype,
		condition:Build_condition_slice(cfg_skill_effect.Condition),
		begin_time:cfg_skill_effect.Begin_time,
		end_time:cfg_skill_effect.End_time,
		tag:cfg_skill_effect.Tag,
	}
	var i,j,k int8
	//效果参数组 是否修改
	var p1 [][]int32
	p2 := make(map[int32]int32,0)
	var p3,p4,p5 int32
	var p6 []int32
	for i=0;i<UNIT_CFG_EFFECT_PARAM_MAX;i++ {
		if u.cfg_effect_param_update[i].id <= 0 {
			break
		}
		if u.cfg_effect_param_update[i].id == id {
			for j=0;j<BATTLE_PARAM_MAX1;j++ {
				var tp []int32
				for k=0;k<BATTLE_PARAM_MAX2;k++ {
					if u.cfg_effect_param_update[i].param1[j][k] == 0 {
						break
					}
					tp = append(tp,u.cfg_effect_param_update[i].param1[j][k])
				}
				if len(tp) <= 0 {
					break
				}
				p1 = append(p1,tp)
			}

			for j=0;j<BATTLE_PARAM_MAX1;j++ {
				if u.cfg_effect_param_update[i].param2[j][0] == 0 {
					break
				}
				p2[u.cfg_effect_param_update[i].param2[j][0]] = u.cfg_effect_param_update[i].param2[j][1]
			}
			p3 = u.cfg_effect_param_update[i].param3
			p4 = u.cfg_effect_param_update[i].param4
			p5 = u.cfg_effect_param_update[i].param5
			for j=0;j<BATTLE_PARAM_MAX1;j++ {
				if u.cfg_effect_param_update[i].param6[j] == 0 {
					break
				}
				p6 = append(p6,u.cfg_effect_param_update[i].param6[j])
			}
		}
	}
	if len(p1) == 0 {
		for i=0;i<BATTLE_PARAM_MAX1;i++ {
			var tp []int32
			for k=0;k<BATTLE_PARAM_MAX2;k++ {
				if cfg_skill_effect.Param1[i][k] <= 0 {
					break
				}
				tp = append(tp,cfg_skill_effect.Param1[i][k])
			}
			if len(tp) <= 0{
				break
			}
			p1 = append(p1,tp)
		}
	}
	if len(p2) == 0 {
		for i=0;i<BATTLE_PARAM_MAX1;i++ {
			if cfg_skill_effect.Param2[i][0] <= 0 {
				break
			}
			p2[cfg_skill_effect.Param2[i][0]] = cfg_skill_effect.Param2[i][1]
		}
	}
	if p3 == 0 {
		p3 = cfg_skill_effect.Param3
	}
	if p4 == 0 {
		p4 = cfg_skill_effect.Param4
	}
	if p5 == 0 {
		p5 = cfg_skill_effect.Param5
	}
	if len(p6) == 0 {
		for i=0;i<BATTLE_PARAM_MAX1;i++ {
			if cfg_skill_effect.Param6[i] == 0 {
				break
			}
			p6 = append(p6,cfg_skill_effect.Param6[i])
		}
	}

	params := &Cfg_skill_effect_params {
		p1:p1,
		p2:p2,
		p3:p3,
		p4:p4,
		p5:p5,
		p6:p6,
	}

	tmp.params = params
	return tmp
}

func (u *ShmUnit) BuildCfgSkillChild(id int32) *Cfg_skill_child {
	cfg_skill_child := Get_config_skill_child(id)
	if cfg_skill_child == nil {
        log.Printf("config skill child %d not found.",id)
        return nil
	}

	if u.Type == UNIT_TYPE_MONSTER && cfg_skill_child.Type == 2 {
		//怪物不允许使用半时停技能
		log.Printf("monster can't use skill child type = 2")
		return nil
	}

	tmp := &Cfg_skill_child {
		Type:cfg_skill_child.Type,
	}
	var i,j int8
	//子技能效果id组 是否修改
	var effect []int32
	for i=0;i<UNIT_CFG_EFFECT_MAX;i++ {
		if u.cfg_effect_update[i].id <= 0 {
			break
		}
		if u.cfg_effect_update[i].id == id {
			//被修改
			for j=0;j<SKILL_CHILD_EFFECT_MAX;j++ {
				if u.cfg_effect_update[i].effect[j] <= 0 {
					break
				}
				effect = append(effect,u.cfg_effect_update[i].effect[j])
			}
		}
	}
	if len(effect) == 0 {
		for i=0;i<SKILL_CHILD_EFFECT_MAX;i++ {
			if cfg_skill_child.Effect[i] <= 0 {
				break
			}
			effect = append(effect,cfg_skill_child.Effect[i])
		}
	}
	if len(effect) == 0 {
		return nil
	}

	for _,v := range effect {
		tmp_effect := u.BuildCfgSkillEffect(v)
		if tmp_effect == nil {
			return nil
		}
		tmp.skill_effect = append(tmp.skill_effect,tmp_effect)
	}

	return tmp
}

func (u *ShmUnit) BuildCfgSkillCard(id int32) *Cfg_skill_card {
	if id == 0 {
		return nil
	}
	cfg_card := Get_config_skill_card(id)
    if cfg_card == nil {
        log.Printf("config skill card %d not found.",id)
        return nil
    }
    tmp := &Cfg_skill_card {
        id:cfg_card.Id,
        target:cfg_card.Target,
        tag:cfg_card.Tag,
        weight:cfg_card.Weight,
    }
	var i int8
	if cfg_card.Skill1.Ids[0] > 0 {
		tmp_skill := &Cfg_skill {
			tag:cfg_card.Tag,
			cd_time:cfg_card.Skill1.Cd_time,
		}
		for i=0;i<CARD_SKILL_MAX;i++{
			if cfg_card.Skill1.Ids[i] <= 0 {
				break
			}
			//
			tmp_skill_child := u.BuildCfgSkillChild(cfg_card.Skill1.Ids[i])
			if tmp_skill_child == nil {
				return nil
			}
			//
			tmp_skill.skill_child = append(tmp_skill.skill_child,tmp_skill_child)
		}
		tmp.skill1 = tmp_skill
	} else {
		log.Printf("config skill card %d skill1 not found.",id)
		return nil
	}

	if cfg_card.Skill2.Ids[0] > 0 {
		tmp_skill := &Cfg_skill {
			tag:cfg_card.Tag,
			cd_time:cfg_card.Skill2.Cd_time,
		}
		for i=0;i<CARD_SKILL_MAX;i++{
			if cfg_card.Skill2.Ids[i] <= 0 {
				break
			}
			//
			tmp_skill_child := u.BuildCfgSkillChild(cfg_card.Skill2.Ids[i])
			if tmp_skill_child == nil {
				return nil
			}
			//
			tmp_skill.skill_child = append(tmp_skill.skill_child,tmp_skill_child)
		}
		tmp.skill2 = tmp_skill
	}

	if cfg_card.Skill3.Ids[0] > 0 {
		tmp_skill := &Cfg_skill {
			tag:cfg_card.Tag,
			cd_time:cfg_card.Skill3.Cd_time,
		}
		for i=0;i<CARD_SKILL_MAX;i++{
			if cfg_card.Skill3.Ids[i] <= 0 {
				break
			}
			//
			tmp_skill_child := u.BuildCfgSkillChild(cfg_card.Skill3.Ids[i])
			if tmp_skill_child == nil {
				return nil
			}
			//
			tmp_skill.skill_child = append(tmp_skill.skill_child,tmp_skill_child)
		}
		tmp.skill3 = tmp_skill
	}

    return tmp
}

func (u *ShmUnit) BuildCfgSkillPassive(id int32) (*Cfg_skill_passive,int8) {
	var i int8
	cfg_passive := Get_config_skill_passive(id)
	if cfg_passive == nil {
		log.Printf("Get config skill passive id %d not found.",id)
		return nil,0
	}
	passive := &Cfg_skill_passive{
		id:cfg_passive.Id,
		cd_time:cfg_passive.Cd_time,
		begin_time:0,
	}
	for i=0;i<CARD_SKILL_MAX;i++ {
		if cfg_passive.Skill[i] <= 0 {
			break
		}
		tmp_skill_child := u.BuildCfgSkillChild(cfg_passive.Skill[i])
		if tmp_skill_child == nil {
			return nil,0
		}
		passive.skill_child = append(passive.skill_child,tmp_skill_child)
	}
	if len(passive.skill_child) <= 0 {
		return nil,0
	}

	return passive,cfg_passive.Type
}
